package com.lx.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;

/**
 * @author 段誉
 * @create 2019-06-26 13:29
 */
public class JwtUser implements UserDetails {
  private final String id;
  private final String username;
  private final String password;
  private final String email;
  private final Collection<? extends GrantedAuthority> authorities;
  private final Date lastPasswordResetDate;

  public JwtUser(String id, String username, String password, String email, Collection<?
          extends GrantedAuthority> authorities, Date lastPasswordResetDate) {
    this.id = id;
    this.username = username;
    this.password = password;
    this.email = email;
    this.authorities = authorities;
    this.lastPasswordResetDate = lastPasswordResetDate;
  }

  /**
   * 返回分配给用户的角色列表
   * @return
   */
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
  }

  /**
   * 添加@JsonIgnore在后台给前台返回数据时自动隐藏此值
   * @return
   */
  @JsonIgnore
  public String getId() {
    return id;
  }

  @JsonIgnore
  @Override
  public String getPassword() {
    return password;
  }

  @Override
  public String getUsername() {
    return username;
  }

  /**
   * 账号是否未过期
   * @return
   */
  @JsonIgnore
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  /**
   * 账号是否未锁定
   * @return
   */
  @JsonIgnore
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  /**
   * 密码是否未过期
   * @return
   */
  @JsonIgnore
  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  /**
   * 账户是否激活
   * @return
   */
  @JsonIgnore
  @Override
  public boolean isEnabled() {
    return true;
  }

  /**
   * 这个是自定义的，返回上次密码重置日期
   * @return
   */
  @JsonIgnore
  public Date getLastPasswordResetDate() {
    return lastPasswordResetDate;
  }
}
